import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.DragData.FilesList
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.onExternalDrag
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.awt.Dialog

fun main() = application {
    Window(
        onCloseRequest = ::exitApplication,
        title = "apk",
    ) {
//        App()
        deskop()
    }
}

lateinit var viewmodel: MainVm

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun deskop() {
    viewmodel = MainVm()
    var isDraw by remember { mutableStateOf(false) }
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.onExternalDrag(
            onDragStart = {
                isDraw = true
                // 变色
            },
            onDragExit = {
                isDraw = false
                // 恢复并刷新
            },
            onDrop = { state ->
                if(state.dragData is FilesList) {
                    (state.dragData as FilesList).readFiles().forEach {
                        if (it.endsWith(".apk")) {
                           viewmodel.addApk(it)
                        }
                    }
                }
                isDraw = false
            },
        )
    ) {
        // 连接的设备
        LazyColumn {
            items(viewmodel.devices.size) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier.size(300.dp, 40.dp)
                ) {
                    Text(text = "${viewmodel.devices[it].serial} === ${viewmodel.devices[it].state}")
                }
            }
        }
        Button(onClick = {
            // 执行选择
            viewmodel.selectApk()
        }) {
            Text(text = "选择Apk文件", modifier = Modifier.size(100.dp))
        }
        // 文件列表
        LazyColumn {
            items(viewmodel.apkList.size) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier.size(300.dp, 40.dp)
                ) {
                    Text(text = viewmodel.apkList[it].name)
                }
            }
        }
        // 安装
        Button(onClick = {
            // 没有文件，执行选择，有文件就执行安装
            if (viewmodel.apkList.isEmpty()) {

            } else {
                viewmodel.installApk()
            }

        }) {
            Text(text = "安装Apk", modifier = Modifier.size(100.dp, 40.dp).padding(8.dp))
        }
    }
}